package hust.iamk.backup;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.cooliris.app.Res;

import android.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Gallery;
import android.widget.Toast;

public class ServiceBackup extends Service {
	private AmazonS3Client s3Client = new AmazonS3Client(
			new BasicAWSCredentials(Constants.ACCESS_KEY_ID,
					Constants.SECRET_KEY));
	int notificationID = 1;
	int countImages;
	Context mContext;

	public ServiceBackup() {
		// TODO Auto-generated constructor stub
		this.mContext = this;
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		// Get URL Image

		BaseParser baseParser = new BaseParser(
				intent.getStringExtra("URL_BACKUP"));
		List<String> listURL = baseParser.getListUrl();
		for (String str : listURL) {
			GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
					Constants.PICTURE_BUCKET, str);
			URL url = s3Client.generatePresignedUrl(urlRequest);
			try {
				// Log.i("Service", "service" + url.toURI().toString());
				new ImageDownloader().execute(url.toURI().toString());
				sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
						Uri.parse("file://"
								+ Environment.getExternalStorageDirectory())));
				countImages++;
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Toast.makeText(mContext, "Exception " + e.getMessage(),
						Toast.LENGTH_LONG).show();
			}
		}
		stopSelf();
		return Service.START_NOT_STICKY;
	}

	//
	// private void displayNotification() {
	// // ---PendingIntent to launch activity if the user selects
	// // this notification---
	// Intent i = new Intent(this, Gallery.class);
	// i.putExtra("notificationID", notificationID);
	// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
	// NotificationManager nm = (NotificationManager)
	// getSystemService(NOTIFICATION_SERVICE);
	// Notification notif = new Notification(Res.drawable.ic_menu_cloud,
	// "Smart Gallery notification", System.currentTimeMillis());
	// CharSequence from = "Smart Gallery";
	// CharSequence message = "Back up success ";
	// notif.setLatestEventInfo(this, from,
	// message + Integer.toString(countImages) + " images",
	// pendingIntent);
	// // ---100ms delay, vibrate for 250ms, pause for 100 ms and
	// // then vibrate for 500ms---
	// // notif.vibrate = new long[] { 100, 250, 100, 500 };
	// nm.notify(notificationID, notif);
	// }
}
